<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使⽤ JS 闭包，给 10 个 p 标签循环加上点击事件</title>
  <style>
    p {
      width: 100%;
      height: 20px;
    }
  </style>
</head>

<body>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
  <p>1</p>
</body>
<script>
  window.onload = () => {
    var arr = document.getElementsByTagName("p");
    for (let i = 0; i < arr.length; i++) {
      arr[i].onclick = function () {
        console.log('点击充公');
      }
    }
  }

</script>

</html>